﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;

namespace Windows8Accelerator.Core.Xaml.Helpers
{
    public class ShareContractHelper : ShareContractHelperBase
    {
        public override void ShowShareUI()
        {
            DataTransferManager.ShowShareUI(); 
        }


        void dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var args1 = new ShareDataRequestedEventArgs();
            OnDataRequested(args1);

            if (!args1.Cancel)
            {
                DataPackage requestData = args.Request.Data;
                requestData.Properties.Title = args1.Title;
                requestData.Properties.Description = args1.Description;
                if (args1.Uri != null)
                    requestData.SetUri(args1.Uri);
                if (args1.Text != null)
                    requestData.SetText(args1.Text);
                if (args1.Html != null)
                    requestData.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(args1.Html));
            }
        }

        public override void EnableShare()
        {
            var dataTransferManager = DataTransferManager.GetForCurrentView();
            dataTransferManager.DataRequested += dataTransferManager_DataRequested;
            AppHelper.CurrentPage.Unloaded += CurrentPage_Unloaded;
        }

        void CurrentPage_Unloaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            AppHelper.CurrentPage.Unloaded -= CurrentPage_Unloaded;
            var dataTransferManager = DataTransferManager.GetForCurrentView();
            dataTransferManager.DataRequested -= dataTransferManager_DataRequested;
        }
    }
}
